package com.freshfresh.activity.selfcenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.CommentAdapter;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
//地址列表

public class AddressListActivity extends BaseActivity {
	List<Map<String, Object>> listmap_comment;
	Dialog progressDialog;
	CommentAdapter adapter;
	ImageView iv_search;
	
	/**
	 */
	private SwipeMenuListView mPullRefreshListView;
	private TextView tv_title = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.address_list);
		initView();
		initData();
	}

	public void initData() {
		progressDialog = Utils.initProgressDialog(AddressListActivity.this);
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		params.put("productid", "2341");
		params.put("page", "1");
		params.put("page_size", "100");
		params.put("sorttype", "");
		String ad_url = UrlConstants.product_comment_url;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String, Object> map = (Map<String, Object>) Utils
									.parseJsonStr(arg0).get("data");
							listmap_comment = (List<Map<String, Object>>) map
									.get("review_list");
							adapter = new CommentAdapter(
									AddressListActivity.this, listmap_comment);
							mPullRefreshListView.setAdapter(adapter);
							
							
							
							SwipeMenuCreator creator = new SwipeMenuCreator() {

								@Override
								public void create(SwipeMenu menu) {
									// create "open" item
									SwipeMenuItem openItem = new SwipeMenuItem(
											getApplicationContext());
									// set item background
									openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
											0xCE)));
									// set item width
									openItem.setWidth(dp2px(90));
									// set item title
									openItem.setTitle("Open");
									// set item title fontsize
									openItem.setTitleSize(18);
									// set item title font color
									openItem.setTitleColor(Color.WHITE);
									// add to menu
									menu.addMenuItem(openItem);

									// create "delete" item
									SwipeMenuItem deleteItem = new SwipeMenuItem(
											getApplicationContext());
									// set item background
									deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
											0x3F, 0x25)));
									// set item width
									deleteItem.setWidth(dp2px(90));
									// set a icon
									deleteItem.setIcon(R.drawable.ic_delete);
									// add to menu
									menu.addMenuItem(deleteItem);
								}
							};
							// set creator
							mPullRefreshListView.setMenuCreator(creator);
							
							
							mPullRefreshListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
								@Override
								public void onMenuItemClick(int position, SwipeMenu menu, int index) {
//									ApplicationInfo item = mAppList.get(position);
									switch (index) {
									case 0:
										// open
										showToast(AddressListActivity.this, "点击了打开");
//										open(item);
										break;
									case 1:
										// delete
										showToast(AddressListActivity.this, "点击了删除");
//										delete(item);
//										mAppList.remove(position);
//										mAdapter.notifyDataSetChanged();
										break;
									}
								}
							});
							
							// set SwipeListener
							mPullRefreshListView.setOnSwipeListener(new OnSwipeListener() {
								
								@Override
								public void onSwipeStart(int position) {
									// swipe start
								}
								
								@Override
								public void onSwipeEnd(int position) {
									// swipe end
								}
							});
							

						} else {

						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(AddressListActivity.this, "获取失败", 1)
								.show();
						progressDialog.dismiss();
					}
				}));

	}

	public void initView() {
		// 得到控件
		mPullRefreshListView = (SwipeMenuListView) findViewById(R.id.pull_refresh_list);
		tv_title = (TextView) findViewById(R.id.tv_title);
		iv_search = (ImageView) findViewById(R.id.iv_search);
		tv_title.setText("我的地址");
		iv_search.setVisibility(View.VISIBLE);
		iv_search.setImageResource(R.drawable.jia_plus);

	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}
}
